package com.guigui.gulimall.coupon.controller;

import com.guigui.common.to.CouponSaveTo;
import com.guigui.common.to.MemberPriceTo;
import com.guigui.common.utils.PageUtils;
import com.guigui.common.utils.R;
import com.guigui.gulimall.coupon.entity.MemberPriceEntity;
import com.guigui.gulimall.coupon.entity.SkuFullReductionEntity;
import com.guigui.gulimall.coupon.entity.SkuLadderEntity;
import com.guigui.gulimall.coupon.entity.SpuBoundsEntity;
import com.guigui.gulimall.coupon.service.MemberPriceService;
import com.guigui.gulimall.coupon.service.SkuFullReductionService;
import com.guigui.gulimall.coupon.service.SkuLadderService;
import com.guigui.gulimall.coupon.service.SpuBoundsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 商品spu积分设置
 *
 * @author xin
 * @email 806748502@qq.com
 * @date 2021-07-23 15:59:52
 */
@RestController
@RequestMapping("coupon/spubounds")
public class SpuBoundsController {

    @Autowired
    private SpuBoundsService spuBoundsService;

    @Autowired
    private SkuLadderService skuLadderService;

    @Autowired
    private SkuFullReductionService skuFullReductionService;

    @Autowired
    private MemberPriceService memberPriceService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("coupon:spubounds:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = spuBoundsService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("coupon:spubounds:info")
    public R info(@PathVariable("id") Long id){
		SpuBoundsEntity spuBounds = spuBoundsService.getById(id);

        return R.ok().put("spuBounds", spuBounds);
    }

    /**
     * 保存
     */
    @Transactional
    @RequestMapping("/saveCouponSaveTo")
    public R saveReductionLadderMemberPrice(@RequestBody List<CouponSaveTo> couponSaveTo){
        couponSaveTo.forEach(c -> {
            // 保存递减信息
            SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
            BeanUtils.copyProperties(c, skuLadderEntity);
            skuLadderService.save(skuLadderEntity);

            // 保存满减信息
            SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
            BeanUtils.copyProperties(c, skuFullReductionEntity);
            skuFullReductionService.save(skuFullReductionEntity);

            // 保存会员价
            List<MemberPriceTo> memberPriceTos = c.getMemberPriceTos();
            List<MemberPriceEntity> memberPriceEntities = memberPriceTos.stream().map(m -> {
                MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                BeanUtils.copyProperties(m, memberPriceEntity);
                return memberPriceEntity;
            }).collect(Collectors.toList());
            memberPriceService.saveBatch(memberPriceEntities);

        });
        return R.ok();
    }

    /**
     * 保存积分信息
     */
    @RequestMapping("/saveBounds")
    public R saveBounds(@RequestBody SpuBoundsEntity spuBounds){
        spuBoundsService.save(spuBounds);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("coupon:spubounds:update")
    public R update(@RequestBody SpuBoundsEntity spuBounds){
		spuBoundsService.updateById(spuBounds);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("coupon:spubounds:delete")
    public R delete(@RequestBody Long[] ids){
		spuBoundsService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
